const app = getApp();
var request = require("../../utils/request.js");
Page({
  data: {
    iconList: [],
    pageNum: 1,
    pageSize: 10,
    gridCol: 3,
    skin: false,
    TabCur: 0,
    baseUrl: '',
    navbarInitTop: 0,
    isFixed: false,
    taskTitles: [],
    taskList: [],
    logoUrl: null,
    imgwidth: 0,
    imgheight: 0
  },

  onLoad: function () {
    wx.showLoading({
      title: '加载中...',
      mask: true
    });
    var that = this
    var url = wx.getStorageSync('baseUrl')
    var logoUrl = wx.getStorageSync('imageBaseUrl') + 'logo.jpg'
    that.setData({
      baseUrl: url,
      logoUrl: logoUrl
    })
    // 页面创建时执行
    request.post('/index/iconlist', {}, function (res) {
        let titles = []
        let titleList = [{
          "typeId": 0,
          "typeLabel": "推荐"
        }]
        if (res.code == 200) {
          titles = res.data
          for (let i = 0; i < titles.length; i++) {
            titleList.push(titles[i])
          }
          that.setData({
            taskTitles: titleList
          })
          that.setData({
            iconList: res.data
          })
        } else {
          wx.showToast({
            title: '导航图标加载失败',
            icon: 'none',
            duration: 1000
          });
        }
      }),
      //加载任务卡片内容
      that.findTasksByTypId(that.data.TabCur)
  },
  //获取当前组件到顶部的高度
  onShow: function () {
    if (this.data.navbarInitTop == 0) {
      wx.createSelectorQuery().select('#navbar').boundingClientRect((rect) => {
        if (rect && rect.top > 0) {
          this.data.navbarInitTop = (rect.top) * 2 + 42;
        }
      }).exec();
    }
  },
  //隐藏加载框
  onReady() {
    wx.hideLoading()
  },
  tabSelect(e) {
    var that = this
    this.setData({
      TabCur: e.currentTarget.dataset.id,
      scrollLeft: (e.currentTarget.dataset.id - 1) * 60
    })
    that.findTasksByTypId(that.data.TabCur);
  },
  //监听屏幕滚动 判断上下滚动
  onPageScroll: function (e) {
    var scrollTop = e.scrollTop;
    var isSatisfy = scrollTop >= this.data.navbarInitTop;
    this.setData({
      isFixed: isSatisfy
    });
  },
  // 跳转到搜索页面
  onclick: function () {
    wx.navigateTo({
      url: '/pages/search/search'
    })
  },

  findTasks: function (e) {
    var that = this
    var typeId = that.data.iconList[e.currentTarget.id].typeId
    if (typeId != 6) {
      that.findTasksByTypId(typeId);
    } else {
      wx.navigateTo({
        url: '/pages/allTasks/allTasks'
      })
    }
  },
  showDetail: function (event) {
    var that = this
    var taskId = event.currentTarget.dataset.val
    //做一个判断
    if ('' != taskId) {
      wx.navigateTo({
        url: '/pages/detail/detail?taskId=' + taskId
      })
    } else {
      wx.showToast({
        title: '任务id为空！无法跳转',
        icon: 'none',
        duration: 1000
      });
    }
  },
  findTasksByTypId: function (tId) {
    var that = this
    //获取分页信息
    var pageNum = that.data.pageNum;
    var pageSize = that.data.pageSize;
    if (tId != 0) {
      var typeId = tId.toString()
      //加载任务卡片内容
      request.post('/index/taskListByTypeId', {
        "pageNum": pageNum,
        "pageSize": pageSize,
        "typeId": typeId
      }, function (res) {
        if (res.code == 200) {
          var taskList = res.data
          console.log(taskList)
          if (taskList.length == 0) {
            wx.showToast({
              title: "没有更多的数据了...",
              icon: 'fail',
              duration: 2000
            })
            //分页失败，分页数据减1
            if (pageNum > 1) {
              that.setData({
                pageNum: --pageNum
              });
              return;
            }
          }
          that.setData({
            taskList: taskList
          })
        } else {
          //如果数据加载失败，则提示
          wx.showToast({
            title: "加载数据失败",
            icon: 'fail',
            duration: 2000
          });
          //分页失败，分页数据回退
          if (pageNum > 1) {
            that.setData({
              pageNum: --pageNum
            });
          }
        }
      });
    } else {
      request.post('/index/taskListByTypeId', {
        "pageNum": pageNum,
        "pageSize": pageSize
      }, function (res) {
        if (res.code == 200) {
          that.setData({
            taskList: res.data
          })
        } else {
          wx.showToast({
            title: '任务信息加载失败',
            icon: 'none',
            duration: 1000
          });
        }
      })
    }
  },
  /**
   * 上拉分页
   */
  onReachBottom: function () {
    //上拉分页,将页码加1，然后调用分页函数findTasksByTypId()
    var that = this;
    var pageNum = that.data.pageNum;
    that.setData({
      pageNum: ++pageNum
    });

    setTimeout(function () {
      wx.showToast({
          title: '加载中..'
        }),
        that.findTasksByTypId('0');
      that.setData({
        title: "数据加载完毕"
      })
    }, 1000)
  },
  //判断图片是否加载失败
  imageLoadErr: function (e) {
    var that = this
    var baseUrl = that.data.baseUrl
    var index = e.currentTarget.dataset.id;
    this.setData({
      [`taskList[${index}].imageUrl`]: baseUrl + "/profile/images/noImage.jpg",
    })
  },
})